﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xushre.WeiBoSDK {

    public partial class OClient2 {

        /// <summary>
        /// 获取用户的关注列表
        /// </summary>
        /// <remarks>参数uid与screen_name二者必选其一，且只能选其一</remarks>
        /// <param name="uid">需要查询的用户UID。</param>
        /// <param name="screen_name">需要查询的用户昵称。</param>
        /// <param name="count">单页返回的记录条数，默认为50，最大不超过200。</param>
        /// <param name="cursor">返回结果的游标，下一页用返回值里的next_cursor，上一页用previous_cursor，默认为0。</param>
        /// <param name="trim_status">返回值中user字段中的status字段开关，0：返回完整status字段、1：status字段仅返回status_id，默认为1。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsFriends(Int64 uid = 0, string screen_name = null, int count = 50, int cursor = 0, int trim_status = 1) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (uid > 0) {
                data.Add("uid", uid);
            } else if (!String.IsNullOrEmpty(screen_name)) {
                data.Add("screen_name", screen_name);
            } else {
                return "{\"error\":\"Parameters are screen_name and UID will choose one of the two, and only one of.\",\"error_code\":9003}";
            }

            data.Add("count", count);
            data.Add("cursor", cursor);
            data.Add("trim_status", trim_status);

            try {
                response = Get(API.OClient2.Friendships.FRIENDS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取两个用户之间的共同关注人列表
        /// </summary>
        /// <param name="uid">需要获取共同关注关系的用户UID。</param>
        /// <param name="suid">需要获取共同关注关系的用户UID，默认为当前登录用户。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="trim_status">返回值中user字段中的status字段开关，0：返回完整status字段、1：status字段仅返回status_id，默认为1。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsFriendsInCommon(Int64 uid, Int64 suid = 0, int count = 50, int page = 1, int trim_status = 1) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("uid", uid);
            if (suid > 0) {
                data.Add("suid", suid);
            }
            data.Add("count", count);
            data.Add("page", page);
            data.Add("trim_status", trim_status);

            try {
                response = Get(API.OClient2.Friendships.Friends.IN_COMMON, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取用户的双向关注列表，即互粉列表
        /// </summary>
        /// <remarks>排序功能目前只支持一种</remarks>
        /// <param name="uid">需要获取双向关注列表的用户UID。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="sort">排序类型，0：按关注时间最近排序，默认为0。
        /// </param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsFriendsBilateral(Int64 uid, int count = 50, int page = 1, int sort = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("uid", uid);
            data.Add("count", count);
            data.Add("page", page);
            data.Add("sort", sort);

            try {
                response = Get(API.OClient2.Friendships.Friends.BILATERAL, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取用户双向关注的用户ID列表，即互粉UID列表
        /// </summary>
        /// <param name="uid">需要获取双向关注列表的用户UID。</param>
        /// <param name="count">单页返回的记录条数，默认为50，最大不超过2000。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="sort">排序类型，0：按关注时间最近排序，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsFriendsBilateralIds(Int64 uid, int count = 50, int page = 1, int sort = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("uid", uid);
            data.Add("count", count);
            data.Add("page", page);
            data.Add("sort", sort);

            try {
                response = Get(API.OClient2.Friendships.Friends.Bilateral.IDS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取用户的粉丝列表
        /// </summary>
        /// <remarks>参数uid与screen_name二者必选其一，且只能选其一；最多返回5000条数据</remarks>
        /// <param name="uid">需要查询的用户UID。</param>
        /// <param name="screen_name">需要查询的用户昵称。</param>
        /// <param name="count">单页返回的记录条数，默认为50，最大不超过200。</param>
        /// <param name="cursor">返回结果的游标，下一页用返回值里的next_cursor，上一页用previous_cursor，默认为0。</param>
        /// <param name="trim_status">返回值中user字段中的status字段开关，0：返回完整status字段、1：status字段仅返回status_id，默认为1。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsFollowers(Int64 uid = 0, string screen_name = null, int count = 50, int cursor = 0, int trim_status = 1) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (uid > 0) {
                data.Add("uid", uid);
            } else if (!String.IsNullOrEmpty(screen_name)) {
                data.Add("screen_name", screen_name);
            } else {
                return "{\"error\":\"Parameters are screen_name and UID will choose one of the two, and only one of.\",\"error_code\":9003}";
            }

            data.Add("count", count);
            data.Add("cursor", cursor);
            data.Add("trim_status", trim_status);

            try {
                response = Get(API.OClient2.Friendships.FOLLOWERS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取用户粉丝的用户UID列表
        /// </summary>
        /// <param name="uid">需要查询的用户UID。</param>
        /// <param name="screen_name">需要查询的用户昵称。</param>
        /// <param name="count">单页返回的记录条数，默认为500，最大不超过5000。</param>
        /// <param name="cursor">返回结果的游标，下一页用返回值里的next_cursor，上一页用previous_cursor，默认为0。</param>
        /// <remarks>参数uid与screen_name二者必选其一，且只能选其一</remarks>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsFollowersIds(Int64 uid = 0, string screen_name = null, int count = 50, int cursor = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (uid > 0) {
                data.Add("uid", uid);
            } else if (!String.IsNullOrEmpty(screen_name)) {
                data.Add("screen_name", screen_name);
            } else {
                return "{\"error\":\"Parameters are screen_name and UID will choose one of the two, and only one of.\",\"error_code\":9003}";
            }

            data.Add("count", count);
            data.Add("cursor", cursor);

            try {
                response = Get(API.OClient2.Friendships.Followers.IDS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取用户的活跃粉丝列表
        /// </summary>
        /// <param name="uid">需要查询的用户UID。</param>
        /// <param name="count">返回的记录条数，默认为20，最大不超过200。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsFollowersActive(Int64 uid, int count = 20) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("uid", uid);
            data.Add("count", count);

            try {
                response = Get(API.OClient2.Friendships.Followers.ACTIVE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前登录用户的关注人中又关注了指定用户的用户列表
        /// </summary>
        /// <param name="uid">指定的关注目标用户UID。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsFriendsChainFollowers(Int64 uid, int count = 50, int page = 1) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("uid", uid);
            data.Add("count", count);
            data.Add("page", page);

            try {
                response = Get(API.OClient2.Friendships.FriendsChain.FOLLOWERS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取两个用户之间的详细关注关系情况
        /// </summary>
        /// <remarks>
        /// 参数source_id与source_screen_name二者必选其一，且只能选其一
        /// 参数target_id与target_screen_name二者必选其一，且只能选其一
        /// </remarks>
        /// <param name="source_id">源用户的UID。</param>
        /// <param name="source_screen_name">源用户的微博昵称。</param>
        /// <param name="target_id">目标用户的UID。</param>
        /// <param name="target_screen_name">目标用户的微博昵称。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsShow(Int64 source_id = 0, string source_screen_name = null, Int64 target_id = 0, string target_screen_name = null) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (source_id > 0) {
                data.Add("source_id", source_id);
            } else if (!String.IsNullOrEmpty(source_screen_name)) {
                data.Add("source_screen_name", source_screen_name);
            } else {
                return "{\"error\":\"Source_ID and source_screen_name a combination of parameters required for one thing, and only one of.\",\"error_code\":9004}";
            }

            if (target_id > 0) {
                data.Add("target_id", target_id);
            } else if (!String.IsNullOrEmpty(target_screen_name)) {
                data.Add("target_screen_name", target_screen_name);
            } else {
                return "{\"error\":\"Parameter target_id and target_screen_name will choose one of the two, and only one of.\",\"error_code\":9005}";
            }

            try {
                response = Get(API.OClient2.Friendships.SHOW, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 关注一个用户
        /// </summary>
        /// <param name="uid">需要关注的用户ID。</param>
        /// <param name="screen_name">需要关注的用户昵称。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsCreate(Int64 uid = 0, string screen_name = null) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (uid > 0) {
                data.Add("uid", uid);
            }

            if (!String.IsNullOrEmpty(screen_name)) {
                data.Add("screen_name", screen_name);
            }

            try {
                response = Post(API.OClient2.Friendships.CREATE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;

        }

        /// <summary>
        /// 取消关注一个用户
        /// </summary>
        /// <param name="uid">需要取消关注的用户ID。</param>
        /// <param name="screen_name">需要取消关注的用户昵称。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsDestroy(Int64 uid = 0, string screen_name = null) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (uid > 0) {
                data.Add("uid", uid);
            }

            if (!String.IsNullOrEmpty(screen_name)) {
                data.Add("screen_name", screen_name);
            }

            try {
                response = Post(API.OClient2.Friendships.DESTORY, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;

        }

        /// <summary>
        /// 更新当前登录用户所关注的某个好友的备注信息
        /// </summary>
        /// <param name="uid">需要修改备注信息的用户UID。</param>
        /// <param name="remark">备注信息，需要URLencode。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsRemarkUpdate(Int64 uid, string remark) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("source_id", uid);
            data.Add("remark", remark);

            try {
                response = Post(API.OClient2.Friendships.Remark.UPDATE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }



        /// <summary>
        /// 获取当前登陆用户好友分组列表
        /// </summary>
        /// <remarks>
        /// 返回的数据按设置的分组顺序排序；
        /// 好友分组上限为20，“未分组”不计入上限；
        /// "未分组"的分组ID为0，此接口不返回“未分组”；
        /// </remarks>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroups() {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            try {
                response = Get(API.OClient2.Friendships.GROUPS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前登录用户某一好友分组的微博列表
        /// </summary>
        /// <param name="list_id">需要查询的好友分组ID，建议使用返回值里的idstr，当查询的为私有分组时，则当前登录用户必须为其所有者。</param>
        /// <param name="since_id">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="max_id">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过200，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="base_app">是否只获取当前应用的数据。0为否（所有数据），1为是（仅当前应用），默认为0。</param>
        /// <param name="feature">过滤类型ID，0：全部、1：原创、2：图片、3：视频、4：音乐，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsTimeline(Int64 list_id, Int64 since_id = 0, Int64 max_id = 0, int count = 50, int page = 1, int base_app = 0, int feature = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("list_id", list_id);
            data.Add("since_id", since_id);
            data.Add("max_id", max_id);
            data.Add("count", count);
            data.Add("page", page);
            data.Add("base_app", base_app);
            data.Add("feature", feature);

            try {
                response = Get(API.OClient2.Friendships.Groups.TIMELINE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前登陆用户某一好友分组的微博ID列表
        /// </summary>
        /// <param name="list_id">需要查询的好友分组ID，建议使用返回值里的idstr，当查询的为私有分组时，则当前登录用户必须为其所有者。</param>
        /// <param name="since_id">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="max_id">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，最大不超过200，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="base_app">是否只获取当前应用的数据。0为否（所有数据），1为是（仅当前应用），默认为0。</param>
        /// <param name="feature">过滤类型ID，0：全部、1：原创、2：图片、3：视频、4：音乐，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsTimelineIds(Int64 list_id, Int64 since_id = 0, Int64 max_id = 0, int count = 50, int page = 1, int base_app = 0, int feature = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("list_id", list_id);
            data.Add("since_id", since_id);
            data.Add("max_id", max_id);
            data.Add("count", count);
            data.Add("page", page);
            data.Add("base_app", base_app);
            data.Add("feature", feature);

            try {
                response = Get(API.OClient2.Friendships.Groups.Timeline.IDS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取某一好友分组下的成员列表
        /// </summary>
        /// <param name="list_id">好友分组ID，建议使用返回值里的idstr。</param>
        /// <param name="count">单页返回的记录条数，默认为50，最大不超过200。</param>
        /// <param name="cursor">分页返回结果的游标，下一页用返回值里的next_cursor，上一页用previous_cursor，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsMembers(Int64 list_id, int count = 50, int cursor = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("list_id", list_id);
            data.Add("count", count);
            data.Add("cursor", cursor);

            try {
                response = Get(API.OClient2.Friendships.Groups.MEMBERS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前登录用户某一好友分组下的成员列表的ID
        /// </summary>
        /// <param name="list_id">好友分组ID，建议使用返回值里的idstr。</param>
        /// <param name="count">单页返回的记录条数，默认为50，最大不超过200。</param>
        /// <param name="cursor">分页返回结果的游标，下一页用返回值里的next_cursor，上一页用previous_cursor，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsMembersIds(Int64 list_id, int count = 50, int cursor = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("list_id", list_id);
            data.Add("count", count);
            data.Add("cursor", cursor);

            try {
                response = Get(API.OClient2.Friendships.Groups.Members.IDS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前登录用户某一好友分组下的成员列表的ID
        /// </summary>
        /// <param name="uids">需要查询的好友分组成员用户UID，最多不超过100个。</param>
        /// <param name="list_id">指定成员所在的好友分组ID，建议使用返回值里的idstr。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsMembersDescription(Int64[] uids, int list_id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (uids.Length > 100) {
                return "{\"error\":\"Parameter must be less than the number of UIDs 100.\",\"error_code\":9014}";
            }

            data.Add("uids", String.Join(",", values: uids));
            data.Add("list_id", list_id);

            try {
                response = Get(API.OClient2.Friendships.Groups.Members.DESCRIPTION, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 判断某个用户是否是当前登录用户指定好友分组内的成员
        /// </summary>
        /// <param name="uid">需要判断的用户的UID。</param>
        /// <param name="list_id">指定的当前用户的好友分组ID，建议使用返回值里的idstr。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsIsMember(Int64 uid, Int64 list_id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("uid", uid);
            data.Add("list_id", list_id);

            try {
                response = Get(API.OClient2.Friendships.Groups.IS_MEMBER, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 批量获取某些用户在当前登录用户指定好友分组中的收录信息
        /// </summary>
        /// <param name="uids">需要获取好友分组信息的用户UID列表，每次不超过50个。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsListed(Int64[] uids) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (uids.Length > 50) {
                return "{\"error\":\"Parameter must be less than the number of UIDs 50.\",\"error_code\":9014}";
            }

            data.Add("uids", String.Join(",", values: uids));

            try {
                response = Get(API.OClient2.Friendships.Groups.LISTED, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前登陆用户某个分组的详细信息
        /// </summary>
        /// <param name="list_id">需要查询的好友分组ID，建议使用返回值里的idstr。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsShow(Int64 list_id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("list_id", list_id);

            try {
                response = Get(API.OClient2.Friendships.Groups.SHOW, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 批量获取好友分组的详细信息
        /// </summary>
        /// <remarks>
        /// 参数list_ids、uids，批量时用逗号分隔，且必须一一对应，uid为list_id的所有者；
        /// 当要查询的分组为私有分组时，当前登录用户必须为分组的所有者；
        /// 当前登录用户不是指定好友分组的所有者时，只能获取所有人可见的公开分组、和组内成员可见的公开分组（此时当前登录用户必须是组内成员）的分组信息；
        /// </remarks>
        /// <param name="list_ids">需要查询的好友分组ID，建议使用返回值里的idstr，每次不超过20个。</param>
        /// <param name="uids">参数list_ids所指的好友分组的所有者UID，多个之间用逗号分隔，每次不超过20个，需与list_ids一一对应。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsShowBatch(Int64[] list_ids, Int64[] uids) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (list_ids.Length > 20) {
                return "{\"error\":\"Parameter must be less than the number of UIDs 20.\",\"error_code\":9015}";
            }

            if (uids.Length > 20) {
                return "{\"error\":\"Parameter must be less than the number of UIDs 20.\",\"error_code\":9014}";
            }

            data.Add("list_ids", String.Join(",", list_ids));
            data.Add("uids", String.Join(",", uids));

            try {
                response = Get(API.OClient2.Friendships.Groups.SHOW_BATCH, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 创建好友分组
        /// </summary>
        /// <remarks>
        /// 每个用户最多能够创建20个好友分组；
        /// 重复创建同名分组将给出错误；
        /// 用户自己创建的分组，相互之间名称不能重复；
        /// </remarks>
        /// <param name="name">要创建的好友分组的名称，不超过10个汉字，20个半角字符。否则自动切取前20个汉字。</param>
        /// <param name="description">要创建的好友分组的描述，不超过70个汉字，140个半角字符。否则自动切取前70个汉字。</param>
        /// <param name="tags">要创建的好友分组的标签，最多不超过10个，每个不超过7个汉字，14个半角字符。如大于7个汉字，将自动去除。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsCreate(string name, string description = null, string[] tags = null) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (Encoding.Default.GetByteCount(name) > 20) {
                name = Tools.isSingle(name) ? name.Substring(0, 10) : name.Substring(0, 20);
            }

            if (!String.IsNullOrEmpty(description) && Encoding.Default.GetByteCount(description) > 140) {
                description = Tools.isSingle(description) ? description.Substring(0, 70) : description.Substring(0, 140);
            }

            string tag = null;

            if (tags != null && tags.Length > 0) {
                string[] temp = new string[10];
                for (int count = 0, i = 0; i < tags.Length && count < 10; i++) {
                    if (Encoding.Default.GetByteCount(tags[i]) > 14) {
                        continue;
                    }
                    temp[count] = tags[i];
                    count++;
                }

                tag = string.Join(",", temp);
            }

            data.Add("name", name);
            data.Add("description", description);
            data.Add("tags", tag);

            try {
                response = Post(API.OClient2.Friendships.Groups.CREATE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 更新好友分组
        /// </summary>
        /// <remarks>
        /// 当前登录用户必须为好友分组的创建者；
        /// 参数name、description、tags，至少需要传其中一个，否则接口报错；
        /// 重复的同名分组将给出错误；
        /// 用户自己创建的分组，相互之间名称不能重复；
        /// </remarks>
        /// <param name="list_id">需要更新的好友分组ID，建议使用返回值里的idstr，只能更新当前登录用户自己创建的分组。</param>
        /// <param name="name">好友分组更新后的名称，不超过8个汉字，16个半角字符。</param>
        /// <param name="description">好友分组更新后的描述，不超过70个汉字，140个半角字符。</param>
        /// <param name="tags">好友分组更新后的标签，最多不超过10个，每个不超过7个汉字，14个半角字符。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsUpdate(Int64 list_id, string name = null, string description = null, string[] tags = null) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (String.IsNullOrEmpty(name) && String.IsNullOrEmpty(description) && (tags == null || tags.Length <= 0)) {
                return "{\"error\":\"Parameter name, description, tags, you need at least one.\",\"error_code\":9016}";
            }


            string tag = null;

            if (tags != null && tags.Length > 0) {
                string[] temp = new string[10];
                for (int count = 0, i = 0; i < tags.Length && count < 10; i++) {
                    if (Encoding.Default.GetByteCount(tags[i]) > 14) {
                        continue;
                    }
                    temp[count] = tags[i];
                    count++;
                }

                tag = string.Join(",", temp);
            }

            data.Add("list_id", list_id);
            data.Add("name", name);
            data.Add("description", description);
            data.Add("tags", tag);

            try {
                response = Post(API.OClient2.Friendships.Groups.UPDATE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 删除好友分组
        /// </summary>
        /// <remarks>只能操作当前登录用户自己的好友分组；</remarks>
        /// <param name="list_id">要删除的好友分组ID，建议使用返回值里的idstr。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsDestory(Int64 list_id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("list_id", list_id);

            try {
                response = Post(API.OClient2.Friendships.Groups.DESTORY, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 添加关注用户到好友分组
        /// </summary>
        /// <remarks>
        /// 好友分组成员上限为500；
        /// 当前登录用户必须是该分组的创建者，且只能添加自己关注的人；
        /// </remarks>
        /// <param name="uid">需要添加的用户的UID。</param>
        /// <param name="list_id">好友分组ID，建议使用返回值里的idstr。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsMembersAdd(Int64 uid, Int64 list_id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("uid", uid);
            data.Add("list_id", list_id);

            try {
                response = Post(API.OClient2.Friendships.Groups.Members.ADD, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 批量添加用户到好友分组
        /// </summary>
        /// <remarks>
        /// 好友分组成员上限为500；
        /// 当前登录用户必须是该分组的创建者，且只能添加自己关注的人；
        /// 返回值result，全部成功和部分成功返回true，全部失败返回false或错误结果；
        /// </remarks>
        /// <param name="list_id">好友分组ID，建议使用返回值里的idstr。</param>
        /// <param name="uids">需要添加的用户的UID，最多不超过30个。</param>
        /// <param name="group_descriptions">添加成员的分组说明，每个说明最多8个汉字，16个半角字符，最多不超过30个，且需与uids参数一一对应。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsMembersAddBatch(Int64 list_id, Int64[] uids, string[] group_descriptions = null) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (uids.Length > 30) {
                return "{\"error\":\"Parameter UIDs must be no more than 30.\",\"error_code\":9017}";
            }

            string descriptions = null;

            if (group_descriptions != null && group_descriptions.Length > 0) {
                string[] temp = new string[30];
                for (int count = 0, i = 0; i < group_descriptions.Length && count < 30; i++) {
                    if (Encoding.Default.GetByteCount(group_descriptions[i]) > 16) {
                        continue;
                    }
                    temp[count] = System.Web.HttpUtility.UrlEncode(group_descriptions[i]);
                    count++;
                }

                descriptions = String.Join(",", temp);
            }

            data.Add("list_id", list_id);
            data.Add("uids", string.Join(",", uids));
            data.Add("group_descriptions", descriptions);

            try {
                response = Post(API.OClient2.Friendships.Groups.Members.ADD_BATCH, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 更新好友分组中成员的分组说明
        /// </summary>
        /// <remarks>只能操作当前登录用户自己的好友分组；</remarks>
        /// <param name="list_id">好友分组ID，建议使用返回值里的idstr。</param>
        /// <param name="uid">需要更新分组成员说明的用户的UID。</param>
        /// <param name="group_description">需要更新的分组成员说明，每个说明最多8个汉字，16个半角字符。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsMembersUpdate(Int64 list_id, Int64 uid, string group_description = null) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (!string.IsNullOrEmpty(group_description) && Encoding.Default.GetByteCount(group_description) > 16) {
                group_description = Tools.isSingle(group_description) ? group_description.Substring(0, 8) : group_description.Substring(0, 16);
            }

            data.Add("list_id", list_id);
            data.Add("uid", uid);
            data.Add("group_description", group_description);

            try {
                response = Post(API.OClient2.Friendships.Groups.Members.UPDATE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 删除好友分组内的关注用户
        /// </summary>
        /// <param name="uid">需要删除的用户的UID。</param>
        /// <param name="list_id">好友分组ID，建议使用返回值里的idstr。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsMembersDestory(Int64 uid, Int64 list_id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("uid", uid);
            data.Add("list_id", list_id);

            try {
                response = Post(API.OClient2.Friendships.Groups.Members.DESTORY, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 调整当前登录用户的好友分组顺序
        /// </summary>
        /// <remarks>“未分组”不可调整顺序</remarks>
        /// <param name="list_ids">调整好顺序后的分组ID列表，以逗号分隔，例：57,38，表示57排第一、38排第二，以此类推。</param>
        /// <param name="count">好友分组数量，必须与用户所有的分组数一致、与分组ID的list_id个数一致。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FriendshipsGroupsOrder(string list_ids, int count) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("list_ids", list_ids);
            data.Add("count", count);

            try {
                response = Post(API.OClient2.Friendships.Groups.ORDER, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }
    }
}
